using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;
using System.Collections;
namespace WindowsGame4
{
    public class GameManager
    {

        public static Random random = new Random();
        public static bool bHasInit=false;
        public static List<GameObject> objects, newObjects;
        public static Texture2D texture, lazor, cockpit, saucer, pixel, turret, turretGun, bullet, rat, gameOver;
        public static int width = 800, height = 480;
        public static Player player;
        public static float dt;
        public static SpriteFont basicFont, gameFont;
        public static ContentManager content;
        public static RenderManager rMan;
        public static PoliceEmitter policeEmitter;
        public static Texture2D[] asteroidTextures, debrisTextures, policeTextures, fireTextures, planetTextures;
        public static Vector2[] policePowers;
        public static int nFires = 4, nAsteroids = 8, nDebris = 20, nPolice = 5, nPlanets=2;
        public static int nObjects, nDrawn;
        public static float asteroidDeviance=1600;
        public static AsteroidEmitter asteroidEmitter;
        public static StarEmitter starEmitter;
        public static SoundEffect explosion, shootRay, land, enemyShoot, turretShoot, thrustSound;
        public static Texture2D hangar;
        public static Vector4 goColor;
        public static void Init(GraphicsDeviceManager graphics, int width, int height)
        {     
            bHasInit = true;
            objects = new List<GameObject>();
            newObjects = new List<GameObject>();
            rMan = new RenderManager(graphics, objects, width, height);
            asteroidTextures = new Texture2D[nAsteroids];
            debrisTextures = new Texture2D[nDebris];
            policeTextures = new Texture2D[nPolice];
            policePowers = new Vector2[5];
            fireTextures = new Texture2D[nFires];
            planetTextures = new Texture2D[nPlanets];
            content = ScreenManager.content;
            explosion = content.Load<SoundEffect>("explosion_asteroids");
            shootRay = content.Load<SoundEffect>("shoot_once_lazer_gun");
            land = content.Load<SoundEffect>("spaceship_landing");
            enemyShoot = content.Load<SoundEffect>("shoot_once_space_gun");
            turretShoot = content.Load<SoundEffect>("shoot_once_turret");
            thrustSound = content.Load<SoundEffect>("spaceship_engine");
            gameFont = content.Load<SpriteFont>("gamefont");
            texture = content.Load<Texture2D>("GameThumbnail");
            basicFont = content.Load<SpriteFont>("BasicFont");
            lazor = content.Load<Texture2D>("lazor");
            cockpit = content.Load<Texture2D>("saucer_pt2");
            saucer = content.Load<Texture2D>("saucer_pt1");
            pixel = content.Load<Texture2D>("pixel");
            turret = content.Load<Texture2D>("turret_piece_sm_2");
            turretGun = content.Load<Texture2D>("turret_piece_sm_1");
            bullet = content.Load<Texture2D>("bullet_1");
            rat = content.Load<Texture2D>("rat_1");
            gameOver = content.Load<Texture2D>("game_over_1");
            hangar = content.Load<Texture2D>("thumbnail");
            for (int i = 0; i < nAsteroids; i++)
                asteroidTextures[i] = content.Load<Texture2D>("asteroid_1_" + (i + 1));
            for (int i = 0; i < nDebris; i++)
                debrisTextures[i] = content.Load<Texture2D>("asteroid_piece_" + (i + 1));
            for (int i = 0; i < nPolice; i++)
                policeTextures[i] = content.Load<Texture2D>("space_ship_" + (i + 1));
            for (int i = 0; i < nFires; i++)
                fireTextures[i] = content.Load<Texture2D>("fire_" + (i + 1));
            planetTextures[0] = content.Load<Texture2D>("planet_earth_1");
            planetTextures[1] = content.Load<Texture2D>("planet_unknown_1");
            policePowers[2] = new Vector2(1,0);
            policePowers[1] = new Vector2(4,10);
            policePowers[0] = policePowers[3] = policePowers[4] = new Vector2(2, 20);
            Reset();
        }
        public static void Reset()
        {
            objects.Clear();
            goColor = new Vector4(1, 1, 1, 0);
            //player
            player = new Player();
            player.pos = new Vector2(0, 0);
            player.moveAcc = 300;
            player.depth = 0;
            player.bMoves = false;
            objects.Add(player);
            //emitter
            asteroidEmitter = new AsteroidEmitter();
            asteroidEmitter.attachTarget = player;
            asteroidEmitter.nEmits = 1;
            asteroidEmitter.maxEmits = 10;
            asteroidEmitter.maxRotSpeed = 50;
            asteroidEmitter.minRotSpeed = -50;
            asteroidEmitter.minDistance = 600;
            asteroidEmitter.maxDistance = 800;
            asteroidEmitter.maxDeviance = asteroidDeviance;
            asteroidEmitter.fadeRate = 0;
            asteroidEmitter.moveSpeed = 50;
            asteroidEmitter.particleLifeTime = 1000;
            objects.Add(asteroidEmitter);

            policeEmitter = new PoliceEmitter();
            policeEmitter.attachTarget = player;
            policeEmitter.maxEmits = 0;
            policeEmitter.SetEmissionRate(.1f);
            policeEmitter.minDistance = 500;
            policeEmitter.maxDistance = 600;
            policeEmitter.maxDeviance = 1000;
            AddObject(policeEmitter);

            starEmitter = new StarEmitter();
            starEmitter.attachTarget = player;
            AddObject(starEmitter);
        }
        public static void AddObject(GameObject obj)
        {
            newObjects.Add(obj);
        }
        public static void SetPoliceLevel(int level)
        {
            if (level <= 0)
                return;
            else
            {
                policeEmitter.nEmits = (float)Math.Ceiling((double)level*.5);
                policeEmitter.maxEmits = level;
            }
        }
        public static Vector2 GetCenter()
        {
            Vector2 center = new Vector2(width / 2, height / 2);
            return center;
        }
        public static void Update(GameTime gameTime)
        {
            List<GameObject> deadObjects = new List<GameObject>();
            dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Physics.Collide(objects);
            foreach (GameObject obj in objects)
            {
                if (obj.bDead)
                    deadObjects.Add(obj);
                else obj.Update(dt);
            }
            foreach (GameObject obj in deadObjects)
            {
                obj.OnDeath();
                objects.Remove(obj);
            }
            foreach (GameObject obj in newObjects)
                objects.Add(obj);
            newObjects.Clear();
            player.Input();
            nObjects = objects.Count;
            foreach(GameObject obj in objects)
                if (obj is ParticleEmitter)
                {
                    ParticleEmitter pe = (ParticleEmitter)obj;
                    nObjects += pe.particles.Count;
                }
        }
        public static void Draw()
        {
            nDrawn = 0;
            rMan.Draw();
        }
    }
}
